function ajaxEventTrigger(event) {
    var ajaxEvent = new CustomEvent(event, {detail: this});
    window.dispatchEvent(ajaxEvent);
}

var oldXHR = window.XMLHttpRequest;

//hook xhr
function newXHR() {
    var realXHR = new oldXHR();
    // realXHR.addEventListener('readystatechange', function () {
    //     ajaxEventTrigger.call(this, 'ajaxReadyStateChange');
    // }, false);
    let oldopen = realXHR.open;

    //realXHR.oldopen=
    function newOpen(method, url, async, user, password) {
        let t = this;
        let oldsrc = url;
        let newsrc = "";
        try {
            if (oldsrc[0] == "/") {  //   / 根目录
                newsrc = "http://localhost:8888/redirect?site=" + window.realUrl.origin + oldsrc;
            } else if (oldsrc[0] == ".") {
                if (oldsrc[1] == ".") {

                } else if (oldsrc[1] == "/") { // ./或者
                    let index = oldsrc.lastIndexOf("/");
                    if (index != -1) {
                        let path = window.realUrl.pathname.substr(0, index)
                        newsrc = "http://localhost:8888/redirect?site=" + window.realUrl.origin + path + oldsrc.substr(1, oldsrc.length);
                    }
                }
            } else if (oldsrc.startsWith("http") || oldsrc.startsWith("https")) {  //绝对路径的请求

                if (oldsrc.startsWith("http://localhost:8888/redirect?site=")) {
                    newsrc = oldsrc;
                } else if (oldsrc.startsWith("http://localhost:8888")) {
                    newsrc = "http://localhost:8888/redirect?site=" + oldsrc;
                } else {
                    newsrc = oldsrc;
                }
            } else {  //同级目录
                let index = window.realUrl.pathname.lastIndexOf("/");
                if (index != -1) {
                    let path = window.realUrl.pathname.substr(0, index)
                    newsrc = "http://localhost:8888/redirect?site=" + window.realUrl.origin + path + "/" + oldsrc;
                }
            }
        } catch (e) {
            console.log(e)
        }
        console.log(this)
        console.log("原", method, oldsrc, async, user)
        console.log("新", method, newsrc, async, user)
        oldopen.call(t, method, newsrc, async, user, password);
    }

    realXHR.open = newOpen;
    return realXHR;
}

window.XMLHttpRequest = newXHR;

// window.addEventListener('ajaxReadyStateChange', function (e) {
//     // let xhr = e.detail;
//     // if (xhr.readyState == 4 && xhr.status == 200) {
//     //  //   console.log("ajax请求成功")
//     //     //  console.log(e.detail.response); // XMLHttpRequest Object
//     //     //     console.log(xhr);
//     // }
// });

/*
var oldcreateElement = document.createElement;

function NEWcreateElement(oldElement) {
    var newElement = oldcreateElement.call(document, oldElement);
    window['oldcreateElement'] = oldcreateElement
    if (oldElement == "script") {
        // console.log('%O', newElement)
        //    console.log(newElement.src)
        var proxy = new Proxy(newElement, {
            set: function (target, key, value) {
                //    console.log(`${key} 被设置为 ${value}`);
                target[key] = value;
            }
        });
        setTimeout(function () {
            //    console.log("script.src", newElement.src)
            //console.log("window.realUrl.pathname",window.realUrl.pathname)
            let scripts = window.oldcreateElement.call(document, "script");
            scripts.type = "text/javascript";
            scripts.language = 'javascript';
            let oldsrc = newElement.src;

            if (oldsrc[0] == "/") {  //   / 根目录
                scripts.src = "http://localhost:8888/redirect?site=" + window.realUrl.origin + oldsrc;
            } else if (oldsrc[0] == ".") {
                if (oldsrc[1] == ".") {

                } else if (oldsrc[1] == "/") { // ./或者
                    let index = oldsrc.lastIndexOf("/");
                    if (index != -1) {
                        let path = window.realUrl.pathname.substr(0, index)
                        scripts.src = "http://localhost:8888/redirect?site=" + window.realUrl.origin + path + oldsrc.substr(1, oldsrc.length);
                    }
                }
            } else if (oldsrc[0] != "/" && oldsrc[0] != ".") {  //同级目录
                let index = oldsrc.lastIndexOf("/");
                if (index != -1) {
                    let path = window.realUrl.pathname.substr(0, index)
                    scripts.src = "http://localhost:8888/redirect?site=" + window.realUrl.origin + path + "/" + oldsrc;
                }
            } else {
                scripts.src = oldsrc;
            }
            document.getElementsByTagName('head')[0].appendChild(scripts);
        }, 1000)
        return newElement;
    } else {
        return newElement;
    }
}

document.createElement = NEWcreateElement
*/

